package com.pureut.equipment.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.pureut.equipment.domain.dto.MaintenancePlanDto;
import com.pureut.equipment.domain.vo.MaintenancePlanVo;
import com.pureut.equipment.domain.vo.RescindVo;
import com.pureut.equipment.service.MaintenancePlanService;
import com.sale.common.core.domain.R;
import com.sale.common.core.web.controller.BaseController;
import com.sale.common.log.annotation.Log;
import com.sale.common.log.enums.BusinessType;
import com.sale.common.mybatis.core.page.PageQuery;
import com.sale.common.mybatis.core.page.TableDataInfo;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.List;

/***
 *
 * Author:保养计划控制层
 * Date:2023/04/23 14:21
 * @DESC
 */
@RestController
@RequestMapping("/maintenance-plan")
public class MaintenancePlanController extends BaseController {

    /**
     * 保养计划service
     */
    @Resource
    MaintenancePlanService maintenancePlanService;

    /**
     * 保养计划列表
     */
    @SaCheckPermission("device:maintenance:plan:list")
    @GetMapping("/list")
    public TableDataInfo<MaintenancePlanDto> list(MaintenancePlanVo maintenancePlanVo, PageQuery pageQuery) {
        return maintenancePlanService.getList(maintenancePlanVo,pageQuery);
    }

    /**
     * 新增
     */
    @SaCheckPermission("device:maintenance:plan:add")
    @Log(title = "新增", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public R<Void> add(@Validated @RequestBody MaintenancePlanVo maintenancePlanVo) throws Exception {
        return toAjax(maintenancePlanService.add(maintenancePlanVo));
    }

    /**
     * 删除
     */
    @SaCheckPermission("device:maintenance:plan:remove")
    @Log(title = "删除", businessType = BusinessType.DELETE)
    @DeleteMapping("/removePlan/{id}")
    public R<Void> delete(@PathVariable("id") Long id) throws Exception {
        return toAjax(maintenancePlanService.removePlan(id));
    }

    /**
     * 修改
     */
    @SaCheckPermission("device:maintenance:plan:edit")
    @Log(title = "删除", businessType = BusinessType.DELETE)
    @PutMapping("/updatePlan")
    public R<Void> updatePlan(@RequestBody MaintenancePlanVo maintenancePlanVo) throws Exception {
        return toAjax(maintenancePlanService.updatePlan(maintenancePlanVo));
    }

    /**
     * 查看
     */
    @SaCheckPermission("device:maintenance:plan:query")
    @GetMapping("/getView/{id}")
    public R<MaintenancePlanDto> getView(@PathVariable("id") Long id) {
        return R.ok(maintenancePlanService.getView(id));
    }

    /**
     * 启用
     */
    @SaCheckPermission("device:maintenance:plan:enable")
    @GetMapping("/enable/{id}")
    public R<Void> enable(@PathVariable("id") Long id) throws Exception {
        return toAjax(maintenancePlanService.enable(id));
    }

    /**
     * 停用
     */
    @SaCheckPermission("device:maintenance:plan:stop")
    @GetMapping("/stop/{id}")
    public R<Void> stop(@PathVariable("id") Long id) {
        return toAjax(maintenancePlanService.stop(id));
    }

    /**
     * 解除关联
     */
    @SaCheckPermission("maintenance:plan:rescind")
    @PutMapping("/rescind")
    public R<Void> rescind(@RequestBody RescindVo rescindVo) {
        return toAjax(maintenancePlanService.rescind(rescindVo));
    }

    /**
     * 根据计划开始时间和结束时间定时修改计划状态(定时任务调用)
     */
    @PostMapping("/updateMaintenanceStatusByTime")
    public R<Void> updateStatusByTime() throws ParseException {
        return toAjax(maintenancePlanService.updateMaintenanceStatusByTime());
    }
}
